package com.huika.dealontop.control.myinfo.activity;

import android.content.DialogInterface;
import android.content.Intent;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.reflect.TypeToken;
import com.hk.app.android.lib.encrypt.MD5Security;
import com.huika.dealontop.R;
import com.huika.dealontop.config.UrlConstants;
import com.huika.dealontop.control.base.extend.ActionBarHelper;
import com.huika.dealontop.control.base.extend.BaseActionBarActivity;
import com.huika.dealontop.support.http.FormResultRequest;
import com.huika.dealontop.support.http.JSONWrapAjaxParams;
import com.huika.dealontop.support.http.RequestResult;
import com.huika.dealontop.utils.DialogUtils;
import com.huika.dealontop.utils.UIUtils;
import com.huika.dealontop.views.IOSAlertDialog;
import com.huika.dealontop.views.ToastMsg;
import com.huika.dealontop.views.Util;
import com.huika.dealontop.views.cleardit.ClearableEditText;
import com.huika.huikalib.utils.StringUtils;

/**
 * Author: Victor Wu
 * Email: victor-wu*foxmail.com
 * Time: 2015/11/17 15:38
 * Description: 修改密码
 */
public class ModifiedPasswordActivity extends BaseActionBarActivity implements TextWatcher, Response.Listener<RequestResult<Void>> {

    public static final String USER_ID = "userID";
    private String mUserId;

    private ClearableEditText mEtOldPwd;
    private ClearableEditText mEtNewPwd;
    private ClearableEditText mEtNewPwdRepeat;
    private TextView mBtnConfirm;

    @Override
    public void setRootView() {
        setContentView(R.layout.activity_modified_password);
    }

    @Override
    protected void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            mUserId = intent.getStringExtra(USER_ID);
        }
        setResult(RESULT_CANCELED);
    }

    @Override
    protected void initWidget() {

        ActionBarHelper barHelper = getActionBarHelper();
        barHelper.setTitleMsg(UIUtils.getString(R.string.modified_password_title));

        mEtOldPwd = (ClearableEditText) findViewById(R.id.cet_modified_pwd_old);
        mEtNewPwd = (ClearableEditText) findViewById(R.id.cet_modified_pwd_new);
        mEtNewPwdRepeat = (ClearableEditText) findViewById(R.id.cet_modified_pwd_new_repeat);
        mBtnConfirm = (Button) findViewById(R.id.btn_modified_pwd_confirm);
        mBtnConfirm.setOnClickListener(this);

        mEtOldPwd.addTextChangedListener(this);
        mEtNewPwd.addTextChangedListener(this);
        mEtNewPwdRepeat.addTextChangedListener(this);
    }

    @Override
    public void widgetClick(View v) {
        switch (v.getId()) {
            case R.id.btn_modified_pwd_confirm:
                confirm();
                break;
        }
        super.widgetClick(v);
    }

    private void confirm() {
        String oldPwd = mEtOldPwd.getText().toString().trim();
        String newPwd = mEtNewPwd.getText().toString().trim();
        String newPwdRep = mEtNewPwdRepeat.getText().toString().trim();

        if (StringUtils.isEmpty(oldPwd) || StringUtils.isEmpty(newPwd) || StringUtils.isEmpty(newPwdRep)) {
            ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.password_cant_empty));
            return;
        }
        int ol = oldPwd.length();
        int nl = newPwd.length();
        if(ol < 6 || ol > 16 || nl < 6 || nl > 16){
            ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.new_password_rule));
            return;
        }

        if (!Util.isTextIdentical(newPwd, newPwdRep)) {
            ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.two_password_input));
            return;
        }
        JSONWrapAjaxParams ajaxParams = new JSONWrapAjaxParams();
        ajaxParams.putCommonTypeParam("userID", mUserId);
        ajaxParams.putCommonTypeParam("oldPassword", MD5Security.getMd5_32_UP(oldPwd));
        ajaxParams.putCommonTypeParam("newPassword", MD5Security.getMd5_32_UP(newPwd));
        FormResultRequest<Void> request = new FormResultRequest<>(UrlConstants.UPDATE_PASSWORD,
                this, this, new TypeToken<RequestResult<Void>>() {
        }.getType());
        request.setRequestParams(ajaxParams);
        showLoadingDialog("");
        executeRequest(request);
    }

    @Override
    public void onResponse(RequestResult<Void> voidRequestResult) {
        dismissLoadingDialog();
        if (voidRequestResult != null) {
            if (voidRequestResult.flag == 1) {
                //新密码设置成功!
                showSuccessDialog();
            } else {
                if (!StringUtils.isEmpty(voidRequestResult.msg)) {
                    ToastMsg.showToastMsg(UIUtils.getContext(), voidRequestResult.msg);
                } else {
                    ToastMsg.showToastMsg(UIUtils.getContext(), UIUtils.getString(R.string.new_password_failure));
                }
            }
        } else {
            ToastMsg.showToastMsg(UIUtils.getContext(), voidRequestResult.msg);
        }
    }

    private void showSuccessDialog() {
        setResult(RESULT_OK);
        //新密码设置成功!
        final IOSAlertDialog dialog = DialogUtils.getInstance().createTitleDialog(this, UIUtils.getString(R.string.new_password_successfully));
        dialog.setConfirmText(UIUtils.getString(R.string.register_confirm));
        dialog.setConfirmClickListener(new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                dialog.dismiss();
                finish();
            }
        });
        dialog.show();
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    }

    @Override
    public void afterTextChanged(Editable editable) {
        if (Util.isEmpty(mEtOldPwd) || Util.isEmpty(mEtNewPwd) || Util.isEmpty(mEtNewPwdRepeat)) {
            mBtnConfirm.setEnabled(false);
        } else {
            mBtnConfirm.setEnabled(true);
        }
    }

}
